<!DOCTYPE html>
<html>
<head>
<style>
td {
    border: solid black 1px;
}
</style>
    <!-- FIXME: Migrate to ES6 modules -->
    <script src="Node.js"></script>
    <script src="Type.js"></script>
    <script src="ReferenceType.js"></script>
    <script src="Value.js"></script>
    <script src="Expression.js"></script>
    <script src="Rewriter.js"></script>
    <script src="Visitor.js"></script>
    <script src="CreateLiteral.js"></script>
    <script src="CreateLiteralType.js"></script>
    <script src="PropertyAccessExpression.js"></script>

    <script src="AddressSpace.js"></script>
    <script src="AnonymousVariable.js"></script>
    <script src="ArrayRefType.js"></script>
    <script src="ArrayType.js"></script>
    <script src="Assignment.js"></script>
    <script src="AutoWrapper.js"></script>
    <script src="Block.js"></script>
    <script src="BoolLiteral.js"></script>
    <script src="Break.js"></script>
    <script src="CallExpression.js"></script>
    <script src="CallFunction.js"></script>
    <script src="Check.js"></script>
    <script src="CheckLiteralTypes.js"></script>
    <script src="CheckLoops.js"></script>
    <script src="CheckRecursion.js"></script>
    <script src="CheckRecursiveTypes.js"></script>
    <script src="CheckReturns.js"></script>
    <script src="CheckUnreachableCode.js"></script>
    <script src="CheckWrapped.js"></script>
    <script src="Checker.js"></script>
    <script src="CloneProgram.js"></script>
    <script src="CommaExpression.js"></script>
    <script src="ConstexprFolder.js"></script>
    <script src="ConstexprTypeParameter.js"></script>
    <script src="Continue.js"></script>
    <script src="ConvertPtrToArrayRefExpression.js"></script>
    <script src="DoWhileLoop.js"></script>
    <script src="DotExpression.js"></script>
    <script src="DoubleLiteral.js"></script>
    <script src="DoubleLiteralType.js"></script>
    <script src="DereferenceExpression.js"></script>
    <script src="EArrayRef.js"></script>
    <script src="EBuffer.js"></script>
    <script src="EBufferBuilder.js"></script>
    <script src="EPtr.js"></script>
    <script src="EnumLiteral.js"></script>
    <script src="EnumMember.js"></script>
    <script src="EnumType.js"></script>
    <script src="EvaluationCommon.js"></script>
    <script src="Evaluator.js"></script>
    <script src="ExpressionFinder.js"></script>
    <script src="ExternalOrigin.js"></script>
    <script src="Field.js"></script>
    <script src="FindHighZombies.js"></script>
    <script src="FlattenProtocolExtends.js"></script>
    <script src="FlattenedStructOffsetGatherer.js"></script>
    <script src="FloatLiteral.js"></script>
    <script src="FloatLiteralType.js"></script>
    <script src="FoldConstexprs.js"></script>
    <script src="ForLoop.js"></script>
    <script src="Func.js"></script>
    <script src="FuncDef.js"></script>
    <script src="FuncInstantiator.js"></script>
    <script src="FuncParameter.js"></script>
    <script src="FunctionLikeBlock.js"></script>
    <script src="HighZombieFinder.js"></script>
    <script src="IdentityExpression.js"></script>
    <script src="IfStatement.js"></script>
    <script src="IndexExpression.js"></script>
    <script src="InferTypesForCall.js"></script>
    <script src="Inline.js"></script>
    <script src="Inliner.js"></script>
    <script src="InstantiateImmediates.js"></script>
    <script src="IntLiteral.js"></script>
    <script src="IntLiteralType.js"></script>
    <script src="Intrinsics.js"></script>
    <script src="LateChecker.js"></script>
    <script src="Lexer.js"></script>
    <script src="LexerToken.js"></script>
    <script src="LiteralTypeChecker.js"></script>
    <script src="LogicalExpression.js"></script>
    <script src="LogicalNot.js"></script>
    <script src="LoopChecker.js"></script>
    <script src="MakeArrayRefExpression.js"></script>
    <script src="MakePtrExpression.js"></script>
    <script src="NameContext.js"></script>
    <script src="NameFinder.js"></script>
    <script src="NameResolver.js"></script>
    <script src="NativeFunc.js"></script>
    <script src="NativeFuncInstance.js"></script>
    <script src="NativeType.js"></script>
    <script src="NativeTypeInstance.js"></script>
    <script src="NormalUsePropertyResolver.js"></script>
    <script src="NullLiteral.js"></script>
    <script src="NullType.js"></script>
    <script src="OriginKind.js"></script>
    <script src="OverloadResolutionFailure.js"></script>
    <script src="Parse.js"></script>
    <script src="Prepare.js"></script>
    <script src="PropertyResolver.js"></script>
    <script src="Program.js"></script>
    <script src="ProgramWithUnnecessaryThingsRemoved.js"></script>
    <script src="Protocol.js"></script>
    <script src="ProtocolDecl.js"></script>
    <script src="ProtocolFuncDecl.js"></script>
    <script src="ProtocolRef.js"></script>
    <script src="PtrType.js"></script>
    <script src="ReadModifyWriteExpression.js"></script>
    <script src="RecursionChecker.js"></script>
    <script src="RecursiveTypeChecker.js"></script>
    <script src="ResolveNames.js"></script>
    <script src="ResolveOverloadImpl.js"></script>
    <script src="ResolveProperties.js"></script>
    <script src="ResolveTypeDefs.js"></script>
    <script src="Return.js"></script>
    <script src="ReturnChecker.js"></script>
    <script src="ReturnException.js"></script>
    <script src="SPIR-V.js"></script>
    <script src="SPIRVCodegen.js"></script>
    <script src="SPIRVTypeAnalyzer.js"></script>
    <script src="SPIRVVariableAnalyzer.js"></script>
    <script src="StandardLibrary.js"></script>
    <script src="StatementCloner.js"></script>
    <script src="StructLayoutBuilder.js"></script>
    <script src="StructType.js"></script>
    <script src="Substitution.js"></script>
    <script src="SwitchCase.js"></script>
    <script src="SwitchStatement.js"></script>
    <script src="SynthesizeEnumFunctions.js"></script>
    <script src="SynthesizeStructAccessors.js"></script>
    <script src="TrapStatement.js"></script>
    <script src="TypeDef.js"></script>
    <script src="TypeDefResolver.js"></script>
    <script src="TypeOrVariableRef.js"></script>
    <script src="TypeParameterRewriter.js"></script>
    <script src="TypeRef.js"></script>
    <script src="TypeVariable.js"></script>
    <script src="TypeVariableTracker.js"></script>
    <script src="TypedValue.js"></script>
    <script src="UintLiteral.js"></script>
    <script src="UintLiteralType.js"></script>
    <script src="UnificationContext.js"></script>
    <script src="UnreachableCodeChecker.js"></script>
    <script src="VariableDecl.js"></script>
    <script src="VariableRef.js"></script>
    <script src="VisitingSet.js"></script>
    <script src="WSyntaxError.js"></script>
    <script src="WTrapError.js"></script>
    <script src="WTypeError.js"></script>
    <script src="WhileLoop.js"></script>
    <script src="WrapChecker.js"></script>
<script>
    let code = `
struct VertexInput {
    float2 position;
    float3 color;
}

struct VertexOutput {
    float4 wsl_Position;
    float4 color;
}

struct FragmentOutput {
    float4 wsl_Color;
}

vertex VertexOutput vertexShader(VertexInput vertexInput) {
    VertexOutput result;
    result.wsl_Position = float4(vertexInput.position, 0., 1.);
    result.color = float4(vertexInput.color, 1);
    
    int[10] a;
    int[10u] b;
    return result;
}

fragment FragmentOutput fragmentShader(VertexOutput stageIn) {
    FragmentOutput result;
    result.wsl_Color = stageIn.color;
    return result;
}`;

    window.addEventListener("load", function () {
        fetch("spirv.core.grammar.json").then(response => response.json()).then(function (json) {
            let spirv = SPIRV(json);
            let program = prepare("/internal/test", 0, code);
            let result = generateSPIRV(spirv, program);
            return result;
        }).then(function(result) {
            let resultElement = document.getElementById("Result");
            while (resultElement.childNodes.length != 0)
                resultElement.removeChild(resultElement.childNodes[0]);
            let anchor = document.createElement("a");
            let blob = new Blob([result.file], { type: "application/octet-binary" });
            anchor.href = URL.createObjectURL(blob);
            anchor.download = "result.spv";
            anchor.textContent = "Download generated SPIR-V";
            resultElement.appendChild(anchor);

            let table = document.createElement("table");
            let tableHeader = document.createElement("thead");
            let headerRow = document.createElement("tr");
            let nameHeader = document.createElement("th");
            nameHeader.textContent = "Attribute name";
            let locationHeader = document.createElement("th");
            locationHeader.textContent = "Location";
            headerRow.appendChild(nameHeader);
            headerRow.appendChild(locationHeader);
            tableHeader.appendChild(headerRow);
            table.appendChild(tableHeader);
            for (let location of result.locations) {
                let tableRow = document.createElement("tr");
                let nameElement = document.createElement("td");
                nameElement.textContent = location.name;
                let locationElement = document.createElement("td");
                locationElement.textContent = location.location;
                tableRow.appendChild(nameElement);
                tableRow.appendChild(locationElement);
                table.appendChild(tableRow);
            }
            resultElement.appendChild(table);
        }).then(undefined, function () {
            let resultElement = document.getElementById("Result");
            while (resultElement.childNodes.length != 0)
                resultElement.removeChild(resultElement.childNodes[0]);
            resultElement.textContent = "Load failed.";
        });
    });
</script>
</head>
<body>
    <div id="Result">Loading...</div>
</body>
</html>
